/*
 * $Id: AlbumList.java 571 2008-08-24 00:30:07Z jasta00 $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.activity;

import org.devtcg.five.music.R;
import org.devtcg.five.music.widget.AlphabetListView;
import org.devtcg.five.provider.Five;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FilterQueryProvider;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class AlbumList extends Activity
{
	private static final String TAG = "AlbumList";

	private static final String PROJECTION[] = {
		"_id", "name", "full_name", "artwork", "artist"
	};
	
	private ListView mList;
	
	private Cursor getCursor(String selection, String[] args)
	{
		return getContentResolver().query(getIntent().getData(),
		  PROJECTION, selection, args, "a.name COLLATE UNICODE ASC");
	}

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);        
        setContentView(R.layout.album_list);

        Intent i = getIntent();
        if (i.getData() == null)
        	i.setData(Uri.parse("content://org.devtcg.five/media/music/albums/complete"));

        if (i.getAction() == null)
        	i.setAction(Intent.ACTION_VIEW);

        Cursor c = getCursor(null, null);
        startManagingCursor(c);

		ListView list = (ListView)findViewById(R.id.album_list);

		AlbumAdapter adapter = new AlbumAdapter(this,
		  R.layout.album_list_item, c,
		  new String[] { "artwork", "full_name", "artist" },
		  new int[] { R.id.album_cover, R.id.album_name, R.id.artist_name });
		
		list.setAdapter(adapter);
        list.setOnItemClickListener(mClickEvent);
        list.setTextFilterEnabled(true);
    }

    private final OnItemClickListener mClickEvent = new OnItemClickListener()
    {
		public void onItemClick(AdapterView adapter, View v, int pos, long id)
		{
			Intent i = getIntent();

			if (id == -1)
				return;

			Uri chosen = Five.Music.Albums.CONTENT_URI.buildUpon()
			  .appendPath(String.valueOf(id))
			  .build();

			String action = i.getAction();

			Log.d(TAG, "uri=" + chosen.toString());
			Log.d(TAG, "action=" + action);

			if (action.equals(Intent.ACTION_PICK) == true ||
			    action.equals(Intent.ACTION_GET_CONTENT) == true)
			{
				setResult(RESULT_OK, (new Intent()).setData(chosen));
				finish();
			}
			else
			{
				startActivity(new Intent(Intent.ACTION_VIEW, 
				  chosen, AlbumList.this, AlbumSongList.class));
			}
		}
    };

    private class AlbumAdapter extends SimpleCursorAdapter
      implements FilterQueryProvider, SimpleCursorAdapter.ViewBinder
    {
		public AlbumAdapter(Context context, int layout, Cursor c,
		  String[] from, int[] to)
		{
			super(context, layout, c, from, to);
			setFilterQueryProvider(this);
			setViewBinder(this);
		}

		public boolean setViewValue(View v, Cursor c, int col)
		{
			if (v instanceof ImageView)
			{
				String value = c.getString(col);

				ImageView vv = (ImageView)v;

				if (value == null)
					vv.setImageResource(R.drawable.lastfm_cover_small);
				else
					vv.setImageURI(Uri.parse(value));

				return true;
			}

			return false;
		}

		public Cursor runQuery(CharSequence constraint)
		{
			String sel = null;
			String[] args = null;

			Log.d(TAG, "runQuery: " + constraint);

			if (TextUtils.isEmpty(constraint) == false)
			{
				sel = "UPPER(a.name) GLOB ?";

				String wildcard = constraint.toString().replace(' ', '*');
				args = new String[] { "*" + wildcard.toUpperCase() + "*" };
			}

			return AlbumList.this.getCursor(sel, args);
		}

		@Override
		public void changeCursor(Cursor cursor)
		{
			stopManagingCursor(mCursor);

			super.changeCursor(cursor);
			
			if (cursor != null)
				startManagingCursor(cursor);
		}
    }	
}
